package de.gympu.gympulan.data.userdata;

import java.util.Date;
import java.util.Vector;

import de.gympu.gympulan.core.GymPuLan;
import de.gympu.gympulan.core.plugins.gpldatabaseplugin.GPLQuery;
import de.gympu.gympulan.data.userdata.events.GPLUserCreateEvent;
import de.gympu.gympulan.data.userdata.events.GPLUserDeleteEvent;

public class Pupil extends GPLUser {
	private static Vector<Pupil> pupils = new Vector<Pupil>();
	private static boolean loaded = false;
	
	private static Vector<GPLUserCreateEvent> createEvents = new Vector<GPLUserCreateEvent>();
	private static Vector<GPLUserDeleteEvent> deleteEvents = new Vector<GPLUserDeleteEvent>();
	
	protected Pupil(GPLQuery q) {
		super(q.getInt("pupilID"),q);
	}
	
	public boolean equals(Object o) {
		if(o instanceof Pupil) {
			if(((Pupil) o).getLoginName().equals(this.getLoginName())) return true;
			else return false;
		}
		else return false;
	}

	public static void load() {
		if(!loaded) {
			GPLQuery q = GymPuLan.getDatabase().query("SELECT * FROM pupils");
			while(q.hasNext()) {
				pupils.add(new Pupil(q));
			}
			
			loaded = true;
		}
	}

	public void updateLastLogin()  {
		this.lastLogin = (new Date().getTime() / 1000L);
		GymPuLan.getDatabase().update("UPDATE pupils SET lastLogin='" + this.lastLogin + "' WHERE pupilID='" + getUserID() + "'");
	}
}